package utils;

import java.io.File;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;
import java.util.TimerTask;

import exportExcel.ExportExcel;

public class AutoDelete extends TimerTask{
	@Override
	public void run() {
		System.out.println("run");
		this.delete("exp_quanlydoanra");
	}
	
	/**************************************
	 * Method name		: delete
	 * Return type		: void
	 * Description		:xoa file hinh cu
	 * Created date		: Jul 27, 2008
	 * Author			: Honglk
	 **************************************/
	public void delete(String sTenModule) {
		System.out.println("chay phuong thuc delete");
		try {
//			String realPath = UtilsOnScopes.getSession().getServletContext()
//					.getRealPath("");
//			String realPath = UtilsOnScopes.getSession().getServletContext().getContextPath();
			String realPathFolder = ExportExcel.realpath;
			System.out.println("Path foler: " + realPathFolder);
			String fileName = "";
			String dateFileStr = "";
			Calendar calFile = new GregorianCalendar();
			Calendar calPresent = DateUtils
					.DoiChuoiThanhNgayThangGioPhut(DateUtils
							.LayNgayThangGioHienTai());
			try {
				File file = new File(realPathFolder);
				if (file.isDirectory() == true) {
					File[] files = file.listFiles();
					for (File f : files) {
						fileName = f.getName();
						dateFileStr = this.getDateFile(fileName);
						calFile = DateUtils
								.DoiChuoiThanhNgayThangGioPhut(dateFileStr);

						System.out.println(calFile.compareTo(calPresent));

						if (calFile.compareTo(calPresent) == -1) {
							f.delete();
						}

					}
				}
			} catch (Exception ex) {
				//ex.printStackTrace();

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**************************************
	 * Method name		: getDateFile
	 * Return type		: String
	 * Description		:lay ra ngay thang nam tu ten file,dong thoi cong them 10ph
	 * Created date		: Jul 27, 2008
	 * Author			: Honglk
	 **************************************/
	public String getDateFile(String fileName){
		StringTokenizer token = new StringTokenizer(fileName,"-,.");
		String dateStr ="";
		try{
		token.nextToken();
		//nam-thang-ngay; Cong them 10 phut 
		String dateStrTemp = token.nextToken()+"-"+token.nextToken()+"-"
		+token.nextToken()+"-"+token.nextToken();
	
		int phutInt = Integer.parseInt(token.nextToken()) + 10;
		dateStr = dateStrTemp +"-"+String.valueOf(phutInt);
		}catch(Exception ex){
			
		}
		return dateStr;
	}
}
